{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MergeAdjacentImages Notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook contains a few examples of how to use the `MergeAdjacentImages` application from **TubeTK**. First, we will include Python's `os` package as well as `ITK` (Python wrapping). We also set the TubeTK build directory variable `TUBETK_BUILD_DIR`:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If ITK is not installed in your python environment, you need to define the environment variable `ITK_BUILD_DIR` that contains the path to where ITK was built."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We need to find the directory in which TubeTK was build. This is required to find the path to the testing data, and may be also required to find the TubeTK library paths if your python environment does not include it.\n",
    "The environment variable `TubeTK_BUILD_DIR` needs to be defined."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "import numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import itk\n",
    "from itk import TubeTK as ttk\n",
    "from itkwidgets import view"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from matplotlib import cm\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, we load the first input image and show it's origin, spacing, etc.:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Origin:\n",
      "itkPointD2 ([200, 200])\n",
      "Spacing:\n",
      "itkVectorD2 ([2, 2])\n"
     ]
    }
   ],
   "source": [
    "ImageType=itk.Image[itk.F,2]\n",
    "\n",
    "input_image1 = \"Data\\ES0015_Large.mha\"\n",
    "reader0 = itk.ImageFileReader[ImageType].New(FileName=input_image1)\n",
    "reader0.Update()\n",
    "\n",
    "resampler0 = ttk.ResampleImage[itk.F,2].New(Input=reader0.GetOutput(), ResampleFactor=(0.5,0.5))\n",
    "resampler0.Update()\n",
    "im0 = resampler0.GetOutput()\n",
    "\n",
    "print(\"Origin:\")\n",
    "print(im0.GetOrigin())\n",
    "print(\"Spacing:\")\n",
    "print(im0.GetSpacing())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "97cca19f53c14a96ad6c540f50d38c93",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(geometries=[], gradient_opacity=0.22, point_sets=[], rendered_image=<itkImagePython.itkImageF2; proxy o…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "view(im0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's split it into two overlaping images."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {},
   "outputs": [],
   "source": [
    "CropImageFilterType = ttk.CropImage[ImageType,ImageType]\n",
    "\n",
    "crop1 = CropImageFilterType.New(Input=im0, Min=(0,0), Max=(200,200))\n",
    "crop1.Update()\n",
    "im1 = crop1.GetOutput()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d86bbb11da0141f588ca2f565c71e5d5",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(geometries=[], gradient_opacity=0.22, point_sets=[], rendered_image=<itkImagePython.itkImageF2; proxy o…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "view(im1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {},
   "outputs": [],
   "source": [
    "crop2 = CropImageFilterType.New(Input=im0, Min=(50,100), Max=(255,255))\n",
    "crop2.Update()\n",
    "im2 = crop2.GetOutput()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "816f1b75b6514e5aa89a3d1951cb03ca",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(geometries=[], gradient_opacity=0.22, point_sets=[], rendered_image=<itkImagePython.itkImageF2; proxy o…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "view(im2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's merge the images without registration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {},
   "outputs": [],
   "source": [
    "MAIFilterType = ttk.MergeAdjacentImages[ImageType]\n",
    "\n",
    "merge1 = MAIFilterType.New(Input1=im1, Input2=im2)\n",
    "merge1.SetMaxIterations(0)\n",
    "merge1.Update()\n",
    "im12 = merge1.GetOutput()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6cb1574a5cdb400389dd725fad5fc890",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(geometries=[], gradient_opacity=0.22, point_sets=[], rendered_image=<itkImagePython.itkImageF2; proxy o…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "view(im12)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now rotate and translate one image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "metadata": {},
   "outputs": [],
   "source": [
    "TransformType = itk.AffineTransform[itk.D, 2]\n",
    "trans = TransformType.New()\n",
    "trans.Rotate2D( 3.1415926 * 0.02 )\n",
    "\n",
    "ResampleFilterType = ttk.ResampleImage[itk.F, 2]\n",
    "resample2 = ResampleFilterType.New(Input=im2, Transform=trans)\n",
    "resample2.SetLoadTransform( True )\n",
    "resample2.Update()\n",
    "im2T = resample2.GetOutput()\n",
    "\n",
    "merge2 = MAIFilterType.New(Input1=im1, Input2=im2T)\n",
    "merge2.SetBlendUsingAverage( True )\n",
    "merge2.SetMaxIterations(0)\n",
    "merge2.Update()\n",
    "im12T = merge2.GetOutput()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "00e1ef4781c24e649331d579d8b60701",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(geometries=[], gradient_opacity=0.22, point_sets=[], rendered_image=<itkImagePython.itkImageF2; proxy o…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "view(im12T)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now run merge with registration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "metadata": {},
   "outputs": [],
   "source": [
    "merge3 = MAIFilterType.New(Input1=im1, Input2=im2T)\n",
    "merge3.SetMaxIterations(1000)\n",
    "merge3.SetExpectedRotation(0.1)\n",
    "merge3.SetExpectedOffset(1)\n",
    "merge3.SetSamplingRatio(0.5)\n",
    "merge3.SetPadding((10,10))\n",
    "merge3.Update()\n",
    "im12TR = merge3.GetOutput()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5ec293221bf14f708f6260d8e8b1d3f7",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(geometries=[], gradient_opacity=0.22, point_sets=[], rendered_image=<itkImagePython.itkImageF2; proxy o…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "view(im12TR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
